//
//  LyricTool.m
//  LO_MUSIC
//
//  Created by 胡林 on 16/2/27.
//  Copyright © 2016年 杨少锋. All rights reserved.
//


#import "LyricTool.h"
static LyricTool * lyTool = nil;
@implementation LyricTool
//初始化
+(instancetype)shareLyricTool{
    static dispatch_once_t oncetoken;
    dispatch_once(&oncetoken,^{
        lyTool = [[LyricTool alloc] init];
    });
    return lyTool;
}
//初始化歌词
-(void)lyerWithStr:(NSString *)str{
    //如果存放模型的数组中有数据全部移除掉
    if (self.modelArr.count) {
        [self.modelArr removeAllObjects];
    }
    //根据/n切割歌曲，@[@"[02:33.200]停留在冬夜的冷风中", @"[02:37.900]我不是 也不想 装脆弱", @"[02:41.700]只因为你说过 爱是等待是细水长流"]
    if (str.length < 1) {
        return;
    }
    NSArray * lyarr = [str componentsSeparatedByString:@"\n"];
    //@"[02:41.700]只因为你说过 爱是等待是细水长流
    for (NSString *singleStr in lyarr) {
        if (!singleStr.length) {
            continue;
        }
        NSArray * arr = [singleStr componentsSeparatedByString:@"]"];
        NSString * time = nil;
        if ([(NSString *)arr.firstObject length]) {
            time =[(NSString *)arr.firstObject substringFromIndex:1];
        }
        NSString * ly = arr.lastObject;
        //
        LyricTool * tool = [[LyricTool alloc] init];
        tool.lyric = ly;
        tool.time = [self timeFromWithStr:time];
        //添加到数组中
        [self.modelArr addObject:tool];
        
    }
    
    
}
//懒加载
-(NSMutableArray *)modelArr{
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }return _modelArr;
}


-(float)timeFromWithStr:(NSString *)str{
    NSArray * arr = [str componentsSeparatedByString:@":"];
    //03* 60 +38.94
    float time = [arr.firstObject integerValue]*60.0 + [arr.lastObject floatValue];
    return time;
}
//根据时间获取下标
-(NSInteger)indexForTime:(float)time{
    int i = 0;
    for (LyricTool * tool in self.modelArr) {
        
        if (tool.time > time) {
            break;
        }
    i++;
    }
    return i-1;
}






@end
